// 不能执行, 只是对一个查询掌握一个全貌, 可以用那些参数
GET /nginx/_search
{
    "sort": [{"@timestamp": "desc"}], // 排序
    "query": {"match_all": {}}, // 查询
    "_source": ["content"], // source filter
    "from": 0, // 分页: from
    "size": 100, // 分页: size
    "profile": true // 查看怎么执行的查询
    // aggs 聚合
    // highlight 高亮
}

// 脚本字段: 比如订单中有不同的汇率, 需要结合汇率对订单价格进行排序
GET /movie/_search
{
    "query": {"match_all": {}},
    "script_fields": {
        "new_field": {
            "script": {
                "lang": "painless",
                "source": "doc['vote_average'].value*2"
            }
        },
        "new_field2": {
            "script": {
                "lang": "painless",
                "source": "doc['vote_count'].value + '_hello'"
            }
        }
    }
}

// match
GET movie/_search
{
    "query": {
        "match": {
            "title": "Last Christmas" // or
        }
    }
}

// match with operator
GET movie/_search
{
    "query": {
        "match": {
            "title": {
                "query": "Christmas Eve",
                "operator": "AND" // and
            }
        }
    }
}

// 短语搜索: match_phrase, 顺序一致
GET movie/_search
{
    "query": {
        "match_phrase": {
            "overview": {
                "query": "produce gifts",
                "slop": 1 // 表示中间可以有1个字符
            }
        }
    },
    "highlight": { // 高亮显示
        "fields": {
            "overview": {}
        }
    }

}

// 短语搜索: match_phrase, 顺序一致
GET movie/_search
{
    "query": {
        "match_phrase": {
            "overview": {
                "query": "gifts distribute",
                "slop": 1 // 表示中间可以有1个字符
            }
        }
    },
    "highlight": {
        "fields": {
            "overview": {}
        }
    }
}

// query_string
GET movie/_search
{
    "query": {
        "query_string" : {
            "query": "Christmas",
            "default_field": "title"
        }
    }
}

// query_string
GET movie/_search
{
    "query": {
        "query_string" : {
            "query": "title:Christmas"
        }
    }
}

// 支持多个字段和分组
GET movie/_search
{
    "query": {
        "query_string" : {
            "query": "(Christmas AND gifts) OR happy",
            "fields": ["title", "overview"]
        }
    }
}


// term
GET movie/_search
{
    "query": {
        "term": {
            "id": "340816"
        }
    }
}

// term with boost
GET movie/_search
{
    "query": {
        "term": {
            "id": {"value":  "340816", "boost": 2}            
        }
    }
}

// terms
GET movie/_search
{
    "query": {
        "terms": {
            "id": [340816, 51052],
            "boost": 1
        }
    }
}

// terms with boost, 怪怪的, 和term的结构都不大一样了
GET movie/_search
{
    "query": {
        "terms": {
            "id": [340816, 51052],
             "boost": 2
        }
    }
}

// exist
GET movie/_search
{
    "query": {
        "exists": {"field": "release_dates"}
    }
}

// exist not
GET movie/_search
{
    "query": {
        "bool": {
            "must_not": {"exists": {"field": "release_date"}}
        }
    }
}


// ids
GET movie/_search
{
    "query": {
        "ids": {
            "values":[49787, 16358]
        }
    }
}

// named query
GET movie/_search
{
    "query": {
        "bool": {          
            "must": {"match_all": {}},            
            "should": [
                {"term": {"release_dates.year": {"value": "2017", "_name": "2017year"}}}
            ]
        }
    }
}

// minimum_should_match
GET movie/_search
{
    "_source": ["release_dates", "title", "keywords"],
    "size": 200,
    "query": {
        "bool": {
            "must": {
                "range": {
                    "release_dates.year": {
                        "from": 2016,
                        "to": 2017
                    }
                }
            },
            "should": {
                "match": {
                    "title": "Christmas" // or
                }
            },
            "minimum_should_match": 1
        }
    }
}


GET movie/_explain/426469
{
    "query": {
        "bool": {          
            "must": {"match_all": {}},            
            "should": [
                {"term": {"release_dates.year": {"value": "2017", "_name": "2017year"}}}
            ]
        }
    }
}

// collapse 折叠重复内容: 折叠的字段会取其中一条
GET /movie/_search
{
    "query": {"match_all": {}},
    "collapse": {"field": "original_language"}
}


// collapse 折叠重复内容: 二次折叠
GET /movie/_search
{
    "query": {
        "match_all": {}
    },
    "collapse": {
        "field": "original_language",
        "inner_hits": {
            "name": "byCountry",
            "collapse": {
                "field": "release_dates.year"
            },
            "size": 3
        }
    }
}

// highlighting 高亮
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "highlight": {
        "fields": {
            "title": {}
        }
    }
}

// highlighting 高亮: 定制前后标签
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "highlight": {
        "fields": {
            "title": {"pre_tags" : ["<strong>"], "post_tags" : ["</strong>"]}
        }
    }
}

// highlighting 高亮: 定制前后标签
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "highlight": {
        "pre_tags" : ["<strong>"], 
        "post_tags" : ["</strong>"],
        "fields": {
            "title": {}
        }
    }
}

// highlighting 高亮: 全字段高亮
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "highlight": {
        "require_field_match": false, // 默认只有match里面的字段才能高亮, false -> all_fields
        "fields": {
            "title": {},
            "overview": {}
        }
    }
}

// 分页: from + size
GET movie/_search
{
    "query": {
        "match_all": {}
    },
    "size": 20,
    "from": 0
}

// 分页: scroll 第一步, scroll=1m 指定 search context 保存多久, 当超时时, 上下文会被清除
POST /movie/_search?scroll=1m
{
  "query": {
    "match_all": {}
  },
  "size": 2
}
// 分页: scroll 第二步
GET /_search/scroll
{
  "scroll" : "1m",
  // 此 scroll_id  是 第一步/上一步 返回的 _scroll_id, 只有最新的 _scroll_id 可用
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFFBYOGp0WFVCWW13bWlESmlHN1pUAAAAAAAClK0WVURnamR2bWZSMS1qbGEtaDBZSVZ6QQ=="
}
// 更多分页 https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#scroll-search-results


// 排序 sort

// Christmas的查询有好几个分数都是一样的, 但是把年份越新的放前面
// 分数一样, 默认排序
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "sort": ["_score"] // 这是默认值
}
// 分数一样, 年份越新的放前面
GET movie/_search
{
    "query": {
        "match": {
            "title": "Christmas"
        }
    },
    "sort": [
        "_score",
        {
            "release_dates.year": {
                "order": "desc"
            }
        }
    ]
}

// 排序 sort: 对 数组 或者 多值字段 进行排序, 取里面的一个值出来排序, 可以取 max(最大值), min(最小值), sum(求和, array only), avg(平均, array only), median(中值, array only)
DELETE my_index

PUT /my_index/_doc/1
{"product": "chocolate","price": [20, 4]}
PUT /my_index/_doc/2
{"product": "milk","price": [21, 1]}

// sort mode: max
GET /my_index/_search
{
    "sort": {
        "price": {"mode": "max", "order": "desc"}
    }
}

// sort mode: sum
GET /my_index/_search
{
    "sort": {
        "price": {"mode": "sum", "order": "desc"}
    }
}

DELETE my_index

// 排序 sort: 对 missing value 的处理, _first _last 可选, 默认 _last
GET movie/_search
{
    "query": {
        "match_all": {}
    },
    "sort": [
        {"release_dates.year": {"missing": "_first"}}
    ]
}

GET /_cat/indices

POST /nginx/clientip

GET /nginx/_search

GET nginx

// ip
GET /nginx/_search
{
    "query": {
        "bool": {
            "must_not": [
                {
                    "term": {
                        "clientip": "192.168.0.0/16"
                    }
                }
            ]
        }
    }
}

